package com.changpay.zywy.ui.activity.freeze

import android.content.Intent
import android.os.Bundle
import android.view.View
import com.changpay.zywy.R
import com.changpay.zywy.base.BaseFragment
import com.changpay.zywy.base.IView
import com.changpay.zywy.ext.showToast
import com.changpay.zywy.ext.sss
import com.changpay.zywy.http.RetrofitHelper
import kale.ui.view.dialog.EasyDialog
import kotlinx.android.synthetic.main.fragment_freeze_one.*


class FreezeOneFragment : BaseFragment(), IView {
    val FreezeType: Array<String> = arrayOf("冻结所有下级", "冻结个人")
    val accountType: Array<String> = arrayOf("分润账户", "返现账户","分润账户和返现账户")
    var settleFreezeType: String = ""
    var settleAccountType: String = ""
    var member_id: String = ""
    override fun showLoading() {

    }

    override fun hideLoading() {

    }

    override fun showDefaultMsg(msg: String) {
        showToast(msg)

    }

    override fun showMsg(msg: String) {
        showToast(msg)

    }

    override fun showError(errorMsg: String) {
        showToast(errorMsg)
    }


    companion object {
        fun getInstance(): FreezeOneFragment {
            val bundle = Bundle()
            val fragment = FreezeOneFragment()
            fragment.arguments = bundle
            return fragment
        }
    }


    override fun attachLayoutRes(): Int = R.layout.fragment_freeze_one

    override fun lazyLoad() {
    }

    private val onClickListener = View.OnClickListener { view ->
        when (view.id) {
            R.id.tv_daili_name -> {
                Intent(context, FreezePeopleActivity::class.java).run {
                    putExtra("type",1)
                    startActivityForResult(this, 100)
                }
            }
            R.id.tv_dongjie_type-> {
                val dialog = EasyDialog.builder(context!!)
                    .setTitle("冻结类型")
                    // 这里传入的“1”表示默认选择第二个选项
                    .setSingleChoiceItems(
                        FreezeType, -1
                    ) { d, position ->
                        when (position) {
                            0 -> {
                                tv_dongjie_type.text = "冻结所有下级"
                                settleFreezeType = "2"
                            }
                            1 -> {
                                tv_dongjie_type.text = "冻结个人"
                                settleFreezeType = "1"
                            }
                        }
                        d.dismiss()
                    }
                    .build<EasyDialog>()
                dialog.show(fragmentManager)
            }
            R.id.tv_dongjie_zhanghu_type-> {
                val dialog = EasyDialog.builder(context!!)
                    .setTitle("冻结账户类型")
                    // 这里传入的“1”表示默认选择第二个选项
                    .setSingleChoiceItems(
                        accountType, -1
                    ) { d, position ->
                        when (position) {
                            0 -> {
                                tv_dongjie_zhanghu_type.text = "分润账户"
                                settleAccountType = "1"
                            }
                            1 -> {
                                tv_dongjie_zhanghu_type.text = "返现账户"
                                settleAccountType = "2"
                            }
                            2 -> {
                                tv_dongjie_zhanghu_type.text = "分润账户和返现账户"
                                settleAccountType = "3"
                            }
                        }
                        d.dismiss()
                    }
                    .build<EasyDialog>()
                dialog.show(fragmentManager)
            }
            R.id.btn_freeze -> submit()
        }
    }

    override fun initView(view: View) {

        tv_daili_name.setOnClickListener(onClickListener)
        tv_dongjie_type.setOnClickListener(onClickListener)
        tv_dongjie_zhanghu_type.setOnClickListener(onClickListener)
        btn_freeze.setOnClickListener(onClickListener)
    }

    fun submit() {
        if (member_id.isEmpty()) {
            showToast("请选择冻结用户")
            return
        }
        if (settleFreezeType.isEmpty()) {
            showToast("请选择冻结类型")
            return
        }
        if (settleAccountType.isEmpty()) {
            showToast("请选择冻结账户类型")
            return
        }
        val money = et_dongjie_money.text.toString()
        if (money.isEmpty()) {
            showToast("请输入冻结金额")
            return
        }
        val params = HashMap<String, String>()
        params["member_id"] = member_id
        params["freeze_type"] = settleFreezeType
        params["freeze_balance_type"] = settleAccountType
        params["freeze_amount"] = money
        params["key"] = token
        params["client"] = "android"
        RetrofitHelper.service.freeze(params).sss(this)
        {
            showToast(it.result.toString())
            btn_freeze.isEnabled = false
        }

    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == 200) {
            tv_daili_name.text = data?.getStringExtra("member_name")
            tv_daili_phone.text = data?.getStringExtra("member_phone")
            member_id = data?.getStringExtra("member_id")!!
            btn_freeze.isEnabled = true
        }
    }
}






